'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
        return typeof obj;
    } : function (obj) {
        return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj;
    };

var _createClass = function () {
    function defineProperties(target, props) {
        for (var i = 0; i < props.length; i++) {
            var descriptor = props[i];
            descriptor.enumerable = descriptor.enumerable || false;
            descriptor.configurable = true;
            if ("value" in descriptor) descriptor.writable = true;
            Object.defineProperty(target, descriptor.key, descriptor);
        }
    }

    return function (Constructor, protoProps, staticProps) {
        if (protoProps) defineProperties(Constructor.prototype, protoProps);
        if (staticProps) defineProperties(Constructor, staticProps);
        return Constructor;
    };
}();

function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
    }
}

var LunarHelp = function () {
    function LunarHelp(year, month, day) {
        _classCallCheck(this, LunarHelp);

        this.lunarInfo = new Array(0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6, 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0);

        this.nStr1 = new Array('日', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十');
        this.nStr2 = new Array('初', '十', '廿', '三');

        var date = new Date(parseInt(year), parseInt(month) - 1, parseInt(day));

        var i,
            leap = 0,
            temp = 0; //天数
        var baseDate = new Date(1900, 0, 31);
        var offset = (date - baseDate) / 86400000;

        //计算年数
        for (i = 1900; i < 2050 && offset - this.lYearDays(i) > 0; i++) {
            offset -= this.lYearDays(i);
        }

        this.year = i;
        leap = this.leapMonth(i); //闰哪个月
        this.isLeap = false;

        //计算月数
        for (i = 1; i < 13 && offset > 0; i++) {
            //闰月
            if (leap > 0 && i == leap + 1 && this.isLeap == false) {
                --i;
                temp = this.leapDays(this.year);
            } else {
                temp = this.monthDays(this.year, i);
            }

            //解除闰月
            if (this.isLeap == true && i == leap + 1) this.isLeap = false;

            offset -= temp;
        }

        //如果恰好减完了，不是闰月的话月数减1
        if (offset == 0 && leap > 0 && i == leap + 1) if (this.isLeap) {
            this.isLeap = false;
        } else {
            this.isLeap = true;
            --i;
        }

        if (offset < 0) {
            offset += temp;
            --i;
        }

        this.month = i;
        //最后剩余的就是日期
        this.day = offset + 1;
    }

    // 获取y年的总天数


    _createClass(LunarHelp, [{
        key: 'lYearDays',
        value: function lYearDays(year) {
            var i,
                sum = 0;
            for (i = 0x8000; i > 0x8; i >>= 1) {
                sum += this.lunarInfo[year - 1900] & i ? 30 : 29;
            }
            return sum + this.leapDays(year); //最后在加上可能有的闰年的闰月
        }

        //获取闰年闰月的天数 闰大月还是小月

    }, {
        key: 'leapDays',
        value: function leapDays(year) {
            if (this.leapMonth(year)) return this.lunarInfo[year - 1900] & 0x10000 ? 30 : 29; else return 0;
        }

        //获取闰年闰哪个月1-12 ,没闰传回 0

    }, {
        key: 'leapMonth',
        value: function leapMonth(year) {
            return this.lunarInfo[year - 1900] & 0xf;
        }

        //获取y年m月的总天数 正常月

    }, {
        key: 'monthDays',
        value: function monthDays(year, month) {
            return this.lunarInfo[year - 1900] & 0x10000 >> month ? 30 : 29;
        }

        //中文日期

    }, {
        key: 'cDay',
        value: function cDay(d) {
            var s;

            switch (d) {
                case 10:
                    s = '初十';
                    break;
                case 20:
                    s = '二十';
                    break;
                    break;
                case 30:
                    s = '三十';
                    break;
                    break;
                default:
                    s = this.nStr2[Math.floor(d / 10)];
                    s += this.nStr1[d % 10];
            }
            return s;
        }
    }]);
    return LunarHelp;
}();

var SimpleCalendar = function () {
    //构造函数

    function SimpleCalendar(query, options) {
        _classCallCheck(this, SimpleCalendar);

        //默认配置
        this._defaultOptions = {
            width: '500px',
            height: '500px',
            language: 'CH', //语言
            showMark: true, //标记
            timeRange: {
                startYear: 1900,
                endYear: 2099
            },
            timeZone: "", //时区
            mark: []
        };

        //容器
        this.container = document.querySelector(query);

        this._defaultOptions.width = this.container.style.offsetWidth;
        this._defaultOptions.height = this.container.style.offsetHeight;

        //this._options = Object.assign({}, this._defaultOptions, options);

        //得到最终配置
        this._options = this.optionAssign(this._defaultOptions, options);

        this.create();
    }

    //用B更新A的属性 并返回结果


    _createClass(SimpleCalendar, [{
        key: 'optionAssign',
        value: function optionAssign(optionsA, optionsB) {
            for (var key in optionsB) {
                if (_typeof(optionsA[key]) !== 'object') optionsA[key] = optionsB[key]; else {
                    optionsA[key] = this.optionAssign(optionsA[key], optionsB[key]);
                }
            }
            return optionsA;
        }

        //生成日历样式

    }, {
        key: 'create',
        value: function create() {
            var root = this.container;
            root.innerHTML = '<div class="sc-header"></div><div class="sc-body"> </div>';
            root.style.width = this._options.width;
            root.style.height = this._options.height;
            root.className = 'sc-calendar';
            var header = root.querySelector('.sc-header');
            var scbody = root.querySelector('.sc-body');
            //actions
            header.innerHTML = header.innerHTML + '<div class="sc-actions">' + '<div class="sc-mleft"><i class="icon iconfont icon-zuojiantou"></i></div>' + '<select disabled="disabled" class="sc-select-year" name="">' + '</select>' + '<select class="sc-select-month" disabled="disabled" name="">' + '</select>' + '<div class="sc-mright"><i class="icon iconfont icon-xiajiantou"></i></div>' + '</div>';

            scbody.innerHTML = '<div class="sc-week clearfix"></div><div class="sc-days"></div>';
            var week = scbody.querySelector('.sc-week');
            var days = scbody.querySelector('.sc-days');
            for (var i = 0; i < 7; i++) {
                week.innerHTML = week.innerHTML + '<div class="sc-week-item"></div>';
            }
            for (var i = 0; i < 42; i++) {
                days.innerHTML = days.innerHTML + '<div class="sc-item"><div class="day"></div></div>';
            }
            //添加下拉框数据
            this.updateSelect(this.tyear, this.tmonth);
            //刷新日历
            this.update();
        }

        //刷新日历

    }, {
        key: 'update',
        value: function update() {
            var month = arguments.length <= 0 || arguments[0] === undefined ? this.tmonth : arguments[0];
            var year = arguments.length <= 1 || arguments[1] === undefined ? this.tyear : arguments[1];
            this.updateWeek();
            this.addData(year, month);
            this.updateMark(year, month);
            this.updateEvent();
        }

    }, {
        key: 'updateSelect',
        value: function updateSelect(year, month) {
            //下拉框
            var selectYear = this.container.querySelector('.sc-select-year');
            var selectMonth = this.container.querySelector('.sc-select-month');
            selectYear.innerHTML = '';
            for (var i = this._options.timeRange.startYear; i < this._options.timeRange.endYear + 1; i++) {
                selectYear.innerHTML += '<option value="' + i + '">' + i + '年</option>';
            }
            selectMonth.innerHTML = '';
            for (var i = 0; i < 12; i++) {
                var data = this.languageData['months_' + this._options.language];
                selectMonth.innerHTML += '<option value="' + (i + 1) + '">' + data[i] + '</option>';
            }

            selectYear.value = year;
            selectMonth.value = month;
        }

        //刷新星期

    }, {
        key: 'updateWeek',
        value: function updateWeek() {
            var weeks = this.arrayfrom(this.container.querySelectorAll('.sc-week-item'));
            var data = this.languageData['days_' + this._options.language];
            if (!data) {
                console.error('language error!');
            }
            weeks.forEach(function (v, i) {
                v.innerHTML = data[i];
            });
        }

        //添加数据

    }, {
        key: 'addData',
        value: function addData(year, month) {
            var daysElement = this.arrayfrom(this.container.querySelectorAll('.sc-item'));
            var day = new Date(year, month - 1, 1);
            var week = day.getDay();
            if (week == 0) week = 7;

            //计算得到第一个格子的日期
            var thispageStart = new Date(Date.parse(day) - (week) * 24 * 3600 * 1000);

            //对每一个格子遍历
            for (var i = 0; i < 42; i++) {
                daysElement[i].className = 'sc-item';
                var theday = new Date(Date.parse(thispageStart) + i * 24 * 3600 * 1000);
                var writeyear = theday.getFullYear();
                var writeday = theday.getDate();
                var writemonth = theday.getMonth() + 1;
                if (writemonth != month) {
                    daysElement[i].classList.add('sc-othermenth');
                }
                daysElement[i].querySelector('.day').innerHTML = writeday;

                //添加today样式
                if (this.tyear == writeyear && this.tday == writeday && this.tmonth == writemonth) {
                    this.selectDay = daysElement[i];
                    daysElement[i].classList.add("sc-today");
                }
            }
        }

        //刷新标记日期

    }, {
        key: 'updateMark',
        value: function updateMark(year, month) {
            var options = this._options;
            if (options.showMark) {
                var daysElement = this.arrayfrom(this.container.querySelectorAll('.sc-item'));
                var currentmonth = month - 1;
                var data = options.mark;
                if (data) {
                    daysElement.forEach(function (v, i) {
                        var day = +v.querySelector('.day').innerHTML;
                        if (day == 1) currentmonth++;
                        if (data.indexOf(year + '-' + (currentmonth < 10 ? '0' + currentmonth : currentmonth) + '-' + (day < 10 ? '0' + day : day)) >= 0) {
                            v.classList.add('sc-mark');
                        } else {
                            v.classList.remove('sc-mark');
                            v.title = '';
                        }
                    });
                }
            }
        }

    }, {
        key: 'updateEvent',
        value: function updateEvent() {
            var _this = this;
            var daysElement = this.arrayfrom(this.container.querySelectorAll('.sc-item'));
            var container = this.container;
            var calendar = this;
            daysElement.forEach(function (v, i) {
                if ($(v).hasClass("sc-today")) {
                    $(v).click(function () {
                        if ($(this).hasClass("sc-othermenth") || $(this).hasClass("sc-mark")) return;

                        $.ajax({
                            url: io.sign.sign,
                            type: 'get',
                            data: {},
                            dataType: 'json',
                            success: function (data) {
                                console.log(data);
                                // console.log(_this.getSelectedDay(true));
                                // console.log(_this._options.mark);
                                if(data.result=="success"){
                                    calendar.selectDay = v;
                                    var pre = $(container).find('.sc-selected');
                                    if (pre) pre.removeClass('sc-selected');
                                    $(".sc-today").addClass('sc-selected sc-mark');
                                    // console.log($(".sc-today"))
                                    var sday = _this.getSelectedDay(true);
                                    _this._options.mark.push(sday[0] + "-" + sday[1] + "-" + sday[2]);
                                    // setTimeout(function(){
                                    //     window.location.reload();
                                    // },500);
                                    $.ajax({
                                        url: io.sign.signMark,
                                        type: 'get',
                                        data: {
                                            dateTime: sday[0] + "-" + sday[1] + "-" + "01"
                                        },
                                        dataType: 'json',
                                        success: function (data) {
                                            console.log(data);
                                            data.qiandao.forEach(function (a, i) {
                                                if (a) {
                                                    var t = new Date(a.day),
                                                        y = t.getFullYear(),
                                                        m = (t.getMonth() + 1) < 10 ? '0' + (t.getMonth() + 1) : (t.getMonth() + 1),
                                                        d = t.getDate() < 10 ? '0' + t.getDate() : t.getDate();
                                                    data.qiandao[i] = y + '-' + m + '-' + d
                                                }
                                            });
                                            $('.per').signPer({
                                                num: data.qiandao.length,
                                                width: 7 * rem,
                                                height: 7 * rem,
                                                percentage: Math.round(Number(data.paiming)),
                                                speed: 10,
                                                diameter: 3 * rem,
                                                roundedLine: true,//圆角
                                                showRemaining: true,//剩余
                                                fontSize: 1.5 * rem + "px",
                                                fontFamily: 'sans-serif',
                                                fontColor: '#00b0ff',
                                                lineColor: '#00b0ff',
                                                showText: true,
                                                lineWidth: .7 * rem,
                                                remainingLineColor: '#ebeff3'
                                            });
                                            var options = {
                                                width: '100%',
                                                height: '18rem',
                                                showMark: true,
                                                timeRange: {
                                                    startYear: 1949,
                                                    endYear: 2199
                                                },
                                                mark: data.qiandao
                                            };
                                            // var myCalendar = new SimpleCalendar('#calendar', options);
                                        },
                                        error: function () {
                                            $.tips({
                                                content: '服务器忙...',
                                                stayTime: 2000,
                                                type: "warn"
                                            });
                                        }
                                    });
                                }else{
                                    $.tips({
                                        content: '服务器忙...',
                                        stayTime: 2000,
                                        type: "warn"
                                    });
                                    $(".ui-poptips-cnt i").addClass("icon iconfont icon-tishi");
                                }

                            },
                            error: function () {
                                $.tips({
                                    content: '服务器忙...',
                                    stayTime: 2000,
                                    type: "warn"
                                });
                                $(".ui-poptips-cnt i").addClass("icon iconfont icon-tishi");
                            }
                        });
                    });
                }

            });

            var selectYear = container.querySelector('.sc-select-year');
            var selectMonth = container.querySelector('.sc-select-month');
            selectYear.onchange = function () {
                var m = selectMonth.value;
                var y = this.value;
                calendar.update(m, y);
            };

            selectMonth.onchange = function () {
                var y = selectYear.value;
                var m = this.value;
                calendar.update(m, y);
            };

            var monthadd = container.querySelector('.sc-mright');
            var monthsub = container.querySelector('.sc-mleft');

            function getMark(flag) {

                var loadding;
                var currentmonth = selectMonth.value;
                var currentyear = selectYear.value;
                if (flag) {
                    if (currentmonth > 1) currentmonth--; else {
                        currentmonth = 12;
                        selectYear.value = --currentyear;
                    }
                } else {
                    if (currentmonth < 12) currentmonth++; else {
                        currentmonth = 1;
                        selectYear.value = ++currentyear;
                    }
                }
                var sendData = currentyear + "-" + (currentmonth < 10 ? '0' + currentmonth : currentmonth) + '-' + '01';
                $.ajax({
                    url: io.sign.signMark,
                    type: 'get',
                    data: {
                        dateTime: sendData
                    },
                    dataType: 'json',
                    beforeSend: function () {
                        loadding = $.loading({
                            content: '请稍后...'
                        })
                    },
                    success: function (data) {
                        console.log(sendData);
                        console.log(data);

                        data.qiandao.forEach(function (a, i) {
                            if (a) {
                                var t = new Date(a.day),
                                    y = t.getFullYear(),
                                    m = (t.getMonth() + 1) < 10 ? '0' + (t.getMonth() + 1) : (t.getMonth() + 1),
                                    d = t.getDate() < 10 ? '0' + t.getDate() : t.getDate();
                                data.qiandao[i] = y + '-' + m + '-' + d
                            }
                        });

                        _this._options.mark = data.qiandao;
                        loadding.loading("hide");
                        selectMonth.value = currentmonth;
                        calendar.update(currentmonth, currentyear);
                        $('.per').signPer({
                            num: _this._options.mark.length,
                            width: 7 * rem,
                            height: 7 * rem,
                            percentage: Math.round(Number(data.paiming)),
                            speed: 10,
                            diameter: 3 * rem,
                            roundedLine: true,//圆角
                            showRemaining: true,//剩余
                            fontSize: 1.5 * rem + "px",
                            fontFamily: 'sans-serif',
                            fontColor: '#00b0ff',
                            lineColor: '#00b0ff',
                            showText: true,
                            lineWidth: .7 * rem,
                            remainingLineColor: '#ebeff3'
                        });

                    },
                    error: function () {
                        loadding.loading("hide");
                        $.tips({
                            content: '服务器忙...',
                            stayTime: 2000,
                            type: "warn"
                        });
                        $(".ui-poptips-cnt i").addClass("icon iconfont icon-tishi");
                    }
                });
            }

            monthadd.onclick = function () {
                getMark(false);
            };
            monthsub.onclick = function () {
                getMark(true);
            };
        }

        //添加标记

    }, {
        key: 'addMark',
        // value: function addMark(day, info) {
        //     this._options.mark[day] = info;
        //     this.update();
        // }
        value: function addMark(day) {
            this._options.mark.push(day);
            this.update();
        }

        //获取用户点击的日期

    }, {
        key: 'getSelectedDay',
        value: function getSelectedDay(a) {
            var selectYear = this.container.querySelector('.sc-select-year').value;
            var selectMonth = this.container.querySelector('.sc-select-month').value;
            var selectDay = this.selectDay.querySelector('.day').innerHTML;
            if (!a) {
                return new Date(selectYear, selectMonth - 1, selectDay);
            } else {
                return [selectYear, selectMonth, selectDay]
            }
        }

    }, {
        key: 'showMark',
        value: function showMark(s) {
            this._options.showMark = s;
            this.update();
        }
        //将nodelist转为数组

        //nodelist转数组

    }, {
        key: 'arrayfrom',
        value: function arrayfrom(nidelist) {
            var array = [];
            [].forEach.call(nidelist, function (v) {
                array.push(v);
            });
            return array;
        }

    }]);

    return SimpleCalendar;
}();


SimpleCalendar.prototype.languageData = {
    days_CH: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
    months_CH: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
};

SimpleCalendar.prototype.tyear = new Date().getFullYear();
SimpleCalendar.prototype.tmonth = new Date().getMonth() + 1;
SimpleCalendar.prototype.tday = new Date().getDate();
